﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BEELab
{
    public partial class FunctionManagement : System.Web.UI.Page
    {
        /// <summary>
        /// Occures on every page load (first time or Post Back)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Admin"] == null || (bool)Session["Admin"] == false)
            {
                gvExperiment.Columns[3].Visible = false;
                gvExperiment.Columns[4].Visible = false;
                gvExperiment.Columns[9].Visible = false;
            }
        }

        /// <summary>
        /// Creates new function and inserts it to DB. If function aready exists on DB, will duplicate it.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (int.Parse(ddlFunction.SelectedValue) == 0)
            {
                BEELab.Logic.ProgramManagement.FunctionManagement.CreateFunction(txtName.Text, txtDescription.Text, int.Parse(Session["UserID"].ToString()), true, true);
            }
            else
            {
                BEELab.Logic.ProgramManagement.FunctionManagement.DuplicateFunction(int.Parse(ddlFunction.SelectedValue), txtName.Text, txtDescription.Text, int.Parse(Session["UserID"].ToString()), true);
            }
            
            gvExperiment.DataBind();

            txtName.Text = "";
            txtDescription.Text = "";
            ddlFunction.Items.Clear();
            ddlFunction.Items.Add(new ListItem("Blank", "0"));
            ddlFunction.DataBind();
        }

        /// <summary>
        /// Deletes function from DB
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvExperiment_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            BEELab.Logic.ProgramManagement.FunctionManagement.DeleteFunction(Convert.ToInt32(gvExperiment.DataKeys[e.RowIndex].Value));
            ddlFunction.Items.Clear();
            ddlFunction.Items.Add(new ListItem("Blank", "0"));
            ddlFunction.DataBind();
        }

        /// <summary>
        /// Changes function privacy - make it public or private according to user selection
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvExperiment_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "MakePublic")
            {
                BEELab.Logic.ProgramManagement.FunctionManagement.ChangePermission(int.Parse(gvExperiment.DataKeys[int.Parse(e.CommandArgument.ToString())].Value.ToString()), "Public", "");
                gvExperiment.DataBind();
            }
            else if (e.CommandName == "MakePrivate")
            {
                BEELab.Logic.ProgramManagement.FunctionManagement.ChangePermission(int.Parse(gvExperiment.DataKeys[int.Parse(e.CommandArgument.ToString())].Value.ToString()), "Private", "");
                gvExperiment.DataBind();
            }
        }
    }
}